package com.solming.leetcode.question;

//给定一个n个元素有序的（升序）整型数组nums 和一个目标值target ，写一个函数搜索nums中的 target，如果目标值存在返回下标，否则返回 -1
//
//来源：力扣（LeetCode）
//链接：https://leetcode-cn.com/problems/binary-search
public class search704 {

    public static int search(int[] nums, int target) {
        int len = nums.length;
        int start = 0;
        int end = len - 1;
        while (start < end) {
            int mid = start + (end - start) / 2;
            int num = nums[mid];
            if (num == target) {
                return mid;
            } else if (num > target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return -1;

    }
}
